package com.example.elderlycare.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.elderlycare.dao.entity.Common;
import com.example.elderlycare.dao.entity.ConsultRecord;
import com.example.elderlycare.dao.mapper.CommonMapper;
import com.example.elderlycare.dao.mapper.ConsultMapper;
import com.example.elderlycare.service.ConsultService;
import com.example.elderlycare.service.entity.ConsultRecordItem;
import com.example.elderlycare.service.entity.ConsultRecordListResponseBody;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ConsultServiceImpl extends ServiceImpl<ConsultMapper, ConsultRecord> implements ConsultService {
    @Resource
    ConsultMapper consultMapper;

    @Resource
    CommonMapper commonMapper;


    /**
     * queryConsultRecordList 根据参数查询列表
     *
     * @param current      当前页
     * @param pageSize     页面容量
     * @param queryWrapper 查询参数封装
     * @return ConsultRecordListResponseBody
     */
    @Override
    public ConsultRecordListResponseBody queryConsultRecordList(int current, int pageSize, QueryWrapper<ConsultRecord> queryWrapper) {
        QueryWrapper<Common> commonQueryWrapper = new QueryWrapper<>();
        /* type = 19、20 分别对应咨询方式、媒体渠道的字典 */
        commonQueryWrapper.ge("type", 19).le("type", 20);
        List<Common> commonList = commonMapper.selectList(commonQueryWrapper);
        Map<Integer, Common> commonMap = new HashMap<>();
        commonList.forEach((item) -> commonMap.put(item.getId(), item));

        Page<ConsultRecord> page = new Page<>(current, pageSize);
        Page<ConsultRecord> equipTypePage = consultMapper.selectPage(page, queryWrapper);
        ConsultRecordListResponseBody.ConsultRecordListResponseBodyBuilder equipTypeListResponseBodyBuilder = ConsultRecordListResponseBody.builder();
        equipTypeListResponseBodyBuilder.total(Math.toIntExact(equipTypePage.getTotal()));
        List<ConsultRecordItem> items = new ArrayList<>();
        List<ConsultRecord> res = equipTypePage.getRecords();
        res.forEach((item) -> {
            ConsultRecordItem.ConsultRecordItemBuilder consultRecordItemBuilder = ConsultRecordItem.builder().
                    id(item.getId()).
                    name(item.getName()).
                    phone(item.getPhone()).
                    date(item.getDate()).
                    content(item.getContent()).
                    consultMethodId(item.getConsultMethodId()).
                    consultMethod(commonMap.get(item.getConsultMethodId()).getName()).
                    mediaId(item.getMediaId()).
                    media(commonMap.get(item.getMediaId()).getName()).
                    register(item.getRegister());
            items.add(consultRecordItemBuilder.build());
        });
        equipTypeListResponseBodyBuilder.items(items);
        return equipTypeListResponseBodyBuilder.build();
    }
}
